package com.xdtech.trans.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.trans.dao.TransAccountDao;
import com.xdtech.trans.model.TransAccount;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.TransAccountService;
import com.xdtech.trans.service.TransAccountTradeService;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.vo.TransAccountItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-06 17:27:45
 * @since 1.0
 * @see
 */
@Service
public class TransAccountServiceImpl implements TransAccountService {
	private Log log = LogFactory.getLog(TransAccountServiceImpl.class);
	@Autowired
	private TransAccountDao transAccountDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private TransWebSiteService transWebSiteService;
	@Autowired
	private TransAccountTradeService transAccountTradeService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param entity
	 */
	public void save(TransAccount entity) {
		transAccountDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TransAccount> entities) {
		transAccountDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param entity
	 */
	public void delete(TransAccount entity) {
		transAccountDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		transAccountDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param id
	 * @return
	 */
	public TransAccount get(Long id) {
		return transAccountDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @return
	 */
	public List<TransAccount> getAll() {
		return transAccountDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> transAccounts = null;
		long rows = 0;
		if (pg!=null) {
			Page<TransAccount> page = transAccountDao.findPage(pg,"from TransAccount order by createTime desc", values);
			transAccounts = BeanUtils.copyListProperties(
					TransAccountItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TransAccount> transAccountList = transAccountDao.find("from TransAccount order by id desc", values);
			transAccounts = BeanUtils.copyListProperties(
					TransAccountItem.class, transAccountList);
			rows = transAccounts.size();
		}
		results.put("total", rows);
		results.put("rows", transAccounts);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTransAccount(TransAccountItem item) {
		TransAccount transAccount = null;
		if (item.getId()==null) {
			transAccount = new TransAccount();
		}else {
			transAccount = transAccountDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(transAccount, item);
		if (StringUtils.isNotBlank(item.getWebsiteId())) {
			TransWebSite transWebSite = transWebSiteService.get(Long.valueOf(item.getWebsiteId()));
			transAccount.setTransWebSite(transWebSite);
		}
		transAccountDao.save(transAccount);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param transAccountId
	 * @return
	 */
	public TransAccountItem loadTransAccountItem(Long transAccountId) {
		TransAccount transAccount = transAccountDao.get(transAccountId);
		return TransAccountItem.createItem(transAccount);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTransAccountInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTransAccountInfo(List<Long> transAccountIds) {
		for (Long id : transAccountIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-06 17:27:45
	 * @modified by
	 * @return
	 */
	@Override
	public List<TransAccountItem> loadItems() {
		List<TransAccount> transAccounts = getAll();
		return TransAccountItem.createItems(transAccounts);
	}
	/* (非 Javadoc)
	 * accountTypeExitByWebSiteId
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param websiteId
	 * @param accountType
	 * @return
	 * @see com.xdtech.trans.service.TransAccountService#accountTypeExitByWebSiteId(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean accountTypeExitByWebSiteId(String websiteId, String accountType)
	{
		TransAccount transAccount = transAccountDao.findUnique("from TransAccount ta where ta.transWebSite.id=? and accountType=?", Long.valueOf(websiteId),accountType);
 		return transAccount!=null;
	}
	/* (非 Javadoc)
	 * getPayAccountByWebsiteId
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param id
	 * @return
	 * @see com.xdtech.trans.service.TransAccountService#getPayAccountByWebsiteId(java.lang.Long)
	 */
	@Override
	public TransAccount getPayAccountByWebsiteId(Long id)
	{
		TransAccount transAccount = transAccountDao.findUnique("from TransAccount where transWebSite.id=? and accountType=?", id,"1");
		return transAccount;
	}
	/* (非 Javadoc)
	 * getAccountsByWebsiteId
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param valueOf
	 * @return
	 * @see com.xdtech.trans.service.TransAccountService#getAccountsByWebsiteId(java.lang.Long)
	 */
	@Override
	public List<TransAccount> getAccountsByWebsiteId(Long valueOf)
	{
		return transAccountDao.findByHql("from TransAccount where transWebSite.id=? ", valueOf);
	}
	/* (非 Javadoc)
	 * getAccountByPerson
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param creator
	 * @return
	 * @see com.xdtech.trans.service.TransAccountService#getAccountByPerson(java.lang.String)
	 */
	@Override
	public TransAccount getAccountByPerson(String creator)
	{
		return transAccountDao.findUnique("from TransAccount where transWebSite=(select webSite from Person where loginName=?) ", creator);
	}
	

}
